package com.cbs.android.component.graphicsutils.sample;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.cbs.android.component.graphicsutils.GraphicsUtils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void compressBitmap(View view) {
        byte[] bytes = GraphicsUtils.compressBitmap("/sdcard/1.jpg", 300, 80, Bitmap.CompressFormat.JPEG);

//                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//                BitmapFactory.decodeFile("/sdcard/1.jpg").compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
//                byte[] obytes = byteArrayOutputStream.toByteArray();
//                byte[] bytes = GraphicsUtils.compressBitmap(obytes, 616, 90, Bitmap.CompressFormat.JPEG);
//                L.e(TAG, String.format("obytes: %d\tbytes: %d", obytes.length, bytes.length));

//                L.e(TAG, String.format("width: %d\t\theight: %d", bitmap.getWidth(), bitmap.getHeight()));
//                Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/1.jpg");
//                byte[] bytes = GraphicsUtils.compressBitmap(bitmap, 616, 90, Bitmap.CompressFormat.JPEG);

        try {
            FileOutputStream fileOutputStream = new FileOutputStream("/sdcard/2.jpg");
            fileOutputStream.write(bytes);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        byte[] bytes2 = GraphicsUtils.compressBitmap("/sdcard/3.jpg", 300, 70, Bitmap.CompressFormat.JPEG);

//                ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
//                BitmapFactory.decodeFile("/sdcard/3.jpg").compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream2);
//                byte[] obytes2 = byteArrayOutputStream2.toByteArray();
//                byte[] bytes2 = GraphicsUtils.compressBitmap(obytes2, 616, 90, Bitmap.CompressFormat.JPEG);
//                L.e(TAG, String.format("obytes: %d\tbytes: %d", obytes2.length, bytes2.length));

//                Bitmap bitmap2 = BitmapFactory.decodeFile("/sdcard/3.jpg");
//                byte[] bytes2 = GraphicsUtils.compressBitmap(bitmap2, 616, 90, Bitmap.CompressFormat.JPEG);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("/sdcard/4.jpg");
            fileOutputStream.write(bytes2);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
